/**
 * 服务配置serviceMethod.js
 * 版本号： v1.0.0
 */
$(function(){
    $("#serviceMethod_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/serviceMethod/serviceMethodList.do',
        idField: 'id',
        striped:true,
        frozenColumns:[[
            {field:'id',checkbox:true}
        ]],
        columns: [
            [
                {field: 'appKey', title: '应用KEY', width: 85, align: 'center', fixed: true},
                {field: 'method', title: '服务方法', width: 450},
                {field: 'title', title: '服务标题', width: 200, fixed: true},
                {field: 'version', title: '服务版本', width: 60,align: 'center', fixed: true},
                {field: 'needSession', title: '会话较验', width: 60, align: 'center', fixed: true, formatter:function(val){
                    if(val == 1) return "<span style='color: green; font-weight: bold'>是</span>";
                    if(val == 0) return "<span style='color: red; font-weight: bold'>否</span>";
                }},
                {field: 'obsoleted', title: '是否过期', width: 60, align: 'center', fixed: true, formatter:function(val){
                    if(val == 1) return "<span style='color: red; font-weight: bold'>是</span>";
                    if(val == 0) return "<span style='color: green; font-weight: bold'>否</span>";
                }},
                {field: 'timeout', title: '最大超时', width: 60, align: 'center', fixed: true},
                {field: 'status', title: '服务状态', width: 60, align: 'center', fixed: true, formatter:function(val){
                    if(val == 1) return "<span style='color: green; font-weight: bold'>启用</span>";
                    if(val == 0) return "<span style='color: red; font-weight: bold'>禁用</span>";
                }},
                {field: 'createDate', title: '创建时间', width: 135, fixed: true}
            ]
        ],
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                $("#serviceMethod_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#serviceMethod_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            serviceMethod_add();
                        }
                    });
                }
                //修改按钮
                if ("editBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            serviceMethod_edit();
                        }
                    });
                }
                //删除按钮
                if ("delBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            serviceMethod_del();
                        }
                    });
                }
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            serviceMethod_findList();
                        }
                    });
                }
                //详情按钮
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            serviceMethod_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加服务配置
 */
function serviceMethod_add(){
    $("#serviceMethod_dialog").dialog({
        title: '添加服务',
        width: 520,
        modal: true,
        closed: true,
        height: 530,
        iconCls:"icon-custom-add",
        buttons:[{
            text:"添加",
            iconCls:"icon-role-state-ok",
            handler:function(){
                doOptionServiceMethod();
            }
        },{
            text:"取消",
            iconCls:"icon-role-state-no",
            handler:function(){
                $('#serviceMethod_dialog').dialog('close');
            }
        }]
    });
    $("#id").val("");
    $("#method").textbox('setValue',"");
    $("#version").textbox('setValue',"");
    $("#title").textbox('setValue',"");
    $("#serviceMethod_dialog").dialog("move",{left:350,top:10});
    $("#serviceMethod_dialog").dialog('open');

}

/**
 * 删除服务配置
 */
function serviceMethod_del() {
    var row = $("#serviceMethod_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择删除服务配置", timeout: 3000, showType: "slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.method + ' 服务配置吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/serviceMethod/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#serviceMethod_dataList").datagrid("reload", {
                        method:$("#method_").val(),
                        title:$("#title_").val()
                    });
                }
            })
        }
    });
}

/**
 * 修改服务配置
 * @returns {boolean}
 */
function serviceMethod_edit(){
    var row = $("#serviceMethod_dataList").datagrid("getSelected");
    if(row == null){
        $.messager.show({ title:'操作提示', msg:"请选择修改服务配置", timeout:3000, showType:"slide" });
        return;
    }
    $("#serviceMethod_dialog").dialog({
        title: '修改服务',
        width: 520,
        modal: true,
        closed: true,
        height: 530,
        iconCls:"icon-custom-edit",
        buttons:[{
            text:"修改",
            iconCls:"icon-role-state-ok",
            handler:function(){
                doOptionServiceMethod();
            }
        },{
            text:"取消",
            iconCls:"icon-role-state-no",
            handler:function(){
                $('#serviceMethod_dialog').dialog('close');
            }
        }]
    });

    $('#serviceMethod_dialog').dialog('open');
    $("#serviceMethod_dialog").dialog("move",{left:350,top:10});
    $("#serviceMethodForm").form("load", row);
}

/**
 * 服务详情
 */
function serviceMethod_view(iconCls){
    var row = $("#serviceMethod_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的服务详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(fwxq.id , fwxq.url + row.id, iconCls, fwxq.closable, fwxq.title);
}


/**
 * 执行保存操作方法
 */
function doOptionServiceMethod(){
    var params = $("#serviceMethodForm").serialize();
    $("#serviceMethodForm").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/serviceMethod/save.do",
        onSubmit:function() {
            return $(this).form("validate");
        },
        success:function(data) {
            var obj = JSON.parse(data);
            messager(obj);
            $('#serviceMethod_dialog').dialog('close');
            $("#serviceMethod_dataList").datagrid("reload",{});
        }
    });
}

//查询服务配置
function serviceMethod_findList(){
    $("#serviceMethod_dataList").datagrid("reload",{
        method:$("#method_").val(),
        title:$("#title_").val()
    });
    //清楚选中项
    $("#serviceMethod_dataList").datagrid("clearSelections");
}
